import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import org.apache.xml.serializer.Serializer;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.*;

/**
 * @author shun
 */
@SuppressWarnings("ALL")
public class Standard {
    /**
     * 标准类型，国家标准、地方法规...
     */
    @JSONField(ordinal = 1)
    private String category;
    /**
     * 地区
     */
    @JSONField(ordinal = 2)
    private String area;
    /**
     * 专业
     */
    @JSONField(ordinal = 5)
    private String[] specialties;
    /**
     * 规范名
     **/
    @JSONField(ordinal = 3)
    private String name;
    /**
     * 规范编号
     */
    @JSONField(ordinal = 4)
    private String id;
    /**
     * 目录
     */
    @JSONField(ordinal = 6)
    private ArrayList<Standard.Catalog> catalog = new ArrayList<>();

    public Standard() {
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Standard standard = (Standard) o;
        return Objects.equals(category, standard.category) &&
                Objects.equals(name, standard.name) &&
                Objects.equals(id, standard.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(category, name, id);
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getArea() {
        return area;
    }

    public void setArea(String area) {
        this.area = area;
    }

    public String[] getSpecialties() {
        return specialties;
    }

    public void setSpecialties(String[] specialties) {
        this.specialties = specialties;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public ArrayList<Catalog> getCatalog() {
        return catalog;
    }

    public void setCatalog(ArrayList<Catalog> catalog) {
        this.catalog = catalog;
    }

    public static class SubCatalogSerialuzer implements ObjectSerializer {
        @Override
        public void write(JSONSerializer jsonSerializer, Object o, Object o1, Type type, int i) throws IOException {
            ArrayList<Map<String, String>> list = (ArrayList<Map<String, String>>) o;
            ArrayList<String> out = new ArrayList<>();
            for (int j = 0; j < list.size(); j++) {
                out.add(list.get(j).get("title"));
            }
            jsonSerializer.write(out);
        }
    }

    public static class SubCatalogDeserialuzer implements ObjectDeserializer {
        @Override
        public <T> T deserialze(DefaultJSONParser parser, Type type, Object o) {
            List<HashMap<String, String>> result = new ArrayList<>();
            List<String> strs = parser.parseArray(String.class);
            for (String s : strs) {
                HashMap<String, String> map = new HashMap<>();
                map.put("title", s);
                map.put("url", null);
                result.add(map);
            }
            return (T) result;
        }

        @Override
        public int getFastMatchToken() {
            return 0;
        }
    }

    /**
     * 规范目录
     */
    public class Catalog {
        @JSONField(ordinal = 0)
        private String title;
        @JSONField(serialize = false, deserialize = false)
        private String url;
        @JSONField(ordinal = 1,serializeUsing = SubCatalogSerialuzer.class, deserializeUsing = SubCatalogDeserialuzer.class)
        private ArrayList<Map<String, String>> subCatalogs = new ArrayList<>();

        public Catalog(String title, String url) {
            this.title = title;
            this.url = url;
        }

        public Catalog() {
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public ArrayList<Map<String, String>> getSubCatalogs() {
            return subCatalogs;
        }

        public void setSubCatalogs(ArrayList<Map<String, String>> subCatalogs) {
            this.subCatalogs = subCatalogs;
        }
    }

}
